﻿using DMSkin.Core;
using Dt261.Base;
using Dt261.Model;
using Dt261.ViewModel;
using Panuon.UI.Silver;
using Panuon.UI.Silver.Core;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace Dt261.Views
{
    /// <summary>
    /// TestWindow.xaml 的交互逻辑
    /// </summary>
    public partial class PureToneCorrect
    {

        private bool tishiquit = true;



        private Point yuandian = new Point(40, 305);
        public PureToneCorrect()
        {
            InitializeComponent();
            initData(null);

        }




        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {

            if (this.tishiquit)
            {
                var result = MessageBoxX.Show("确认退出？", "提醒", Window.GetWindow(MaskSlider), MessageBoxButton.YesNo, new MessageBoxXConfigurations()
                {
                    MessageBoxStyle = MessageBoxStyle.Modern,
                    MessageBoxIcon = MessageBoxIcon.Warning,
                    ButtonBrush = "#F1C825".ToColor().ToBrush(),
                });

                //MessageBoxResult result = MessageBox.Show("确认退出听力测试？", "询问", MessageBoxButton.YesNo, MessageBoxImage.Question);
                if (result == MessageBoxResult.Yes)
                {

                    e.Cancel = false;


                    PureToneCorrectViewModel m = DataContext as PureToneCorrectViewModel;
                    UninstallBroadcast();

                    m.Dispose();

                }


                //不关闭窗口
                if (result == MessageBoxResult.No)
                    e.Cancel = true;
            }
            else
            {
                e.Cancel = false;

                PureToneCorrectViewModel m = DataContext as PureToneCorrectViewModel;
                

                m.Dispose();
                UninstallBroadcast();
            }


        }

        private void UninstallBroadcast()
        {
            DMSkin.Core.Broadcast.UninstallBroadcast("NEWTESTDATA");
            DMSkin.Core.Broadcast.UninstallBroadcast("CLOSEWINDOW");
            DMSkin.Core.Broadcast.UninstallBroadcast("NEWREPSTAUS");
            DMSkin.Core.Broadcast.UninstallBroadcast("NEWCORRECTDATA");
        }

        private void initData(MemberInfo m)
        {
            this.Closing += Window_Closing;


            PureToneCorrectViewModel model = new PureToneCorrectViewModel();

            model.TestMember = m;
            this.DataContext = model;



            DMSkin.Core.Broadcast.RegisterBroadcast<string>("NEWTESTDATA", (obj) =>
            {
                // this.RspStatus.Background = new SolidColorBrush( Color.FromRgb(0xff, 0xff, 0));
            });

            DMSkin.Core.Broadcast.RegisterBroadcast<string>("CLOSEWINDOW", (obj) =>
            {
                //TestWindowViewModel mo = DataContext as TestWindowViewModel;

                //mo.Dispose();
                this.tishiquit = false;
                this.Close();
            });

            DMSkin.Core.Broadcast.RegisterBroadcast<byte[]>("NEWREPSTAUS", (obj) =>
            {

                ThreadPool.QueueUserWorkItem(delegate
                {
                    SynchronizationContext.SetSynchronizationContext(new
                        DispatcherSynchronizationContext(System.Windows.Application.Current.Dispatcher));
                    SynchronizationContext.Current.Post(pl =>
                    {
                        //byte[] dataBuffer = obj;


                        //int value = dataBuffer[2];

                        //if (value == 1)
                        //{
                        //    this.RspStatus.Background = new SolidColorBrush(Color.FromRgb(0xff, 0xff, 0));
                        //}
                        //else
                        //{
                        //    this.RspStatus.Background = new SolidColorBrush(Color.FromRgb(0xff, 0xff, 0xff));
                        //}

                        //TestWindowViewModel vm = this.DataContext as TestWindowViewModel;

                        ////vm.doResponseOK(dataBuffer);
                    }, null);
                });



            });
        }
       
        

     

        private void Button_MouseDown(object sender, EventArgs e)
        {
            TestWindowViewModel model = this.DataContext as TestWindowViewModel;

            if (model.IsNormalOn)
            {

                model.stopPlay();
            }
            else
            {

                model.startPlay();
            }



        }

        private void Button_MouseUp(object sender, EventArgs e)
        {
            TestWindowViewModel model = this.DataContext as TestWindowViewModel;

            if (model.IsNormalOn)
            {

                model.startPlay();
            }
            else
            {

                model.stopPlay();
            }

        }

        //private void CheckBox_MouseDown(object sender, EventArgs e)
        //{
        //    TestWindowViewModel model = this.DataContext as TestWindowViewModel;

        //    if (model.IsNormalOn)
        //    {
        //        PlayButton.Content = "压下暂停";
        //        model.startPlay();
        //    }
        //    else
        //    {
        //        PlayButton.Content = "压下播放";
        //        model.stopPlay();
        //    }
        //}

      

    }
}
